<template>
  <div id='fromlist'
    @keypress.enter="loginbtn">
    <div class='from-title'>
      <img
        src="/image/logo.45bbb710.png"
        alt="">
      <h3 class='from-h1'>
        商品后台系统
      </h3>
    </div>
    <div class='from-admin'>
      <div class='admin-info'>
        <span
          class='el-icon-user'></span>
        <input type="text"
          v-model='id'
          ref='userid'>
      </div>
    </div>
    <div class='from-pwd'>
      <div class='admin-info'>
        <span
          class='el-icon-lock'></span>
        <input
          :type='falg?"password":"text"'
          v-model='pwd'>
        <span :class=classname
          class='el-icon-view'
          @click='chang'></span>
      </div>
    </div>
    <div class='from-btn'>
      <el-button
        type="primary"
        :loading=loading
        class='admin-btn'
        @click='loginbtn'>
        登录
      </el-button>
    </div>
  </div>
</template>

<script>
import { Button } from 'element-ui'
import Cookie from '../cookies/cookies'
export default {
  name: 'fromlist',
  data() {
    return {
      falg: true,
      classname: 'el-icon-noview',
      id: '',
      pwd: '',
      loading: false,
      index: 0,
    }
  },
  components: {
    [Button.name]: Button,
  },
  methods: {
    chang() {
      this.falg = !this.falg
      if (!this.falg) {
        this.classname = ''
      } else {
        this.classname = 'el-icon-noview'
      }
    },
    loginbtn() {
      if (this.ishave) {
        this.loading = true
        this.$axios({
          method: 'post',
          url: '/login',
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
          },
          params: {
            username: this.id,
            password: this.pwd,
          },
        }).then((res) => {
          if (res.data.status == 0) {
            this.$message.success('登录成功')
            let request = JSON.parse(res.request.response).data
            Cookie.setCookie('password', request.password, 24)
            Cookie.setCookie('userID', request._id, 24)
            Cookie.setCookie('username', request.username, 24)
            this.$router.replace({ path: '/' })
          } else {
            if (this.index == 0) {
              this.$message.warning('账号或密码错误')
              this.index++
              setTimeout(() => {
                this.index = 0
              }, 2000)
            }
          }
          this.loading = false
        })
      } else {
        if (this.index == 0) {
          this.$message.error('请填写账号或密码', 2000)
          this.index++
          setTimeout(() => {
            this.index = 0
          }, 2000)
        }
        this.loading = false
      }
    },
  },
  computed: {
    ishave() {
      if (this.id.length && this.pwd.length) {
        return true
      } else {
        return false
      }
    },
  },
  mounted() {
    this.$refs.userid.focus()
  },
}
</script>

<style lang='less'>
#fromlist {
  width: 420px;
  height: 340px;
  text-align: left;
  display: flex;
  flex-direction: column;
  .from-title {
    margin-bottom: 20px;
    color: #356eea;
    font-size: 30px;
  }
  .from-admin,
  .from-pwd {
    margin-bottom: -15px;
  }
  .from-btn {
    margin-top: -15px;
  }
  div {
    flex: 1;
    text-align: center;
    line-height: 85px;
    .admin-btn {
      width: 400px;
      height: 52px;
      background: -webkit-linear-gradient(left, #2e50e1, #6878f0);
      outline: none;
      border: none;
      color: #fff;
      cursor: pointer;
    }
    img {
      vertical-align: middle;
      margin-right: 10px;
      width: 75px;
      height: 52px;
    }
    .from-h1 {
      display: inline-block;
      vertical-align: middle;
    }
    .admin-info {
      width: 400px;
      height: 54px;
      border: 1px solid #e2e2e2;
      position: relative;
      border-radius: 5px;
      box-sizing: border-box;
      display: inline-block;
      input {
        height: 100%;
        width: 300px;
        font-size: 18px !important;
        position: absolute;
        left: 30px;
        outline: none;
        border: none;
        margin-left: 15px;
        box-sizing: border-box;
        color: #000;
      }
      input:-webkit-autofill,
      textarea:-webkit-autofill,
      select:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px transparent inset !important;
        background-color: transparent;
        background-image: none;
        transition: background-color 50000s ease-in-out 0s; //背景色透明  生效时长  过渡效果  启用时延迟的时间
      }
      input {
        background-color: transparent;
      }
      input[type='password']::-ms-reveal {
        display: none;
      }
      .el-icon-user:before,
      .el-icon-lock:before {
        font-size: 22px;
        color: #818181;
        display: inline-block;
        position: absolute;
        left: 10px;
        top: 15px;
      }
      .el-icon-view:before {
        font-size: 22px;
        color: #818181;
        display: inline-block;
        position: absolute;
        right: 15px;
        top: 15px;
        cursor: pointer;
      }
      .el-icon-noview:after {
        content: '';
        display: inline-block;
        width: 2px;
        height: 20px;
        background: #818181;
        position: absolute;
        right: 25px;
        top: 15px;
        transform: rotate(60deg);
        cursor: pointer;
      }
    }
  }
}
</style>